package com.fly.sms.util;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StringUtil {

	public static String getMD5(String str) {
		if (str == null || str.length() == 0) {
			return "";
		}

		StringBuffer hexString = new StringBuffer();

		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(str.getBytes());
			byte[] hash = md.digest();

			for (int i = 0; i < hash.length; i++) {
				if ((0xff & hash[i]) < 0x10) {
					hexString.append("0"
							+ Integer.toHexString((0xFF & hash[i])));
				} else {
					hexString.append(Integer.toHexString(0xFF & hash[i]));
				}
			}
		} catch (NoSuchAlgorithmException e) {

		}
		return hexString.toString();
	}
	
	public static String delNullStr(String str) {
		if (str == null)
			return "";
		return str;
	}

	public static String utf2gbk(String str) {
		String tmp = "";
		try {
			tmp = new String(str.getBytes("utf-8"), "gbk");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return tmp;
	}
	
	public static String gbk2utf(String str) {
		String tmp = "";
		try {
			tmp = new String(str.getBytes("gbk"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return tmp;
	}
	
	public static String decodeQuotedPrintable(String str) {
		byte ESCAPE_CHAR = '=';
		if (str == null) {
			return null;
		}
		byte[] bytes = str.getBytes();
		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
		for (int i = 0; i < bytes.length; i++) {
			int b = bytes[i];
			if (b == ESCAPE_CHAR) {
				try {
					int u = Character.digit((char) bytes[++i], 16);
					int l = Character.digit((char) bytes[++i], 16);
					if (u == -1 || l == -1) {
						// ����
						return "";
					}
					buffer.write((char) ((u << 4) + l));
				} catch (ArrayIndexOutOfBoundsException e) {
					// ����
					return "";
				}
			} else {
				buffer.write(b);
			}
		}
		bytes = buffer.toByteArray();
		str = "";
		try {
			str = new String(bytes, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}
	
	public static boolean isEmpty(String str) {
		if (str == null)
			return true;
		if (str.length() == 0)
			return true;
		return str==null||"".equals(str)||"null".equals(str);
	}
	
	public static boolean isHttpUrl(String url){
		return (url.toLowerCase().indexOf("http://")>=0);
	}
	
	public static boolean isMobile(String str) {
		if(str==null||str.length()!=11){
			return false;
		}else{
			String regex="(130|131|132|133|134|135|136|137|138|139|150|151|152|153|155|156|157|158|159|180|185|186|187|188|189)\\d{8}";
			Pattern  pattern=Pattern.compile(regex);
			Matcher matcher=pattern.matcher(str);
			return matcher.find();
		}
	}
	
	public static char[] bytesToChars(byte[] bytes,int offset, int length) {
		String s = new String(bytes,offset,length);
		char chars[] = s.toCharArray();
		return chars;
	}
	
	public static String findStr(String source, String start,String end){
		if(source==null)return null;
		int i=0;
		if(!isEmpty(start)){
			i=source.indexOf(start);
			if(i<0)return null;
			source=source.substring(i+start.length());
		}
		if(!isEmpty(end)){
			i=source.indexOf(end);
			if(i<0)return null;
			source=source.substring(0,i);
		}
		return source;
	}
	
	public static String getNumber(String s) {
		Matcher m = Pattern.compile("(\\d+\\.{1}\\d+)").matcher(s);
		m.find();
		return m.group();
	}
	
	public static boolean boolValue(String value){
		return "true".equals(value)||"1".equals(value); 
	}
}